package com.example.rpc;

import io.netty.channel.socket.nio.NioSocketChannel;

import java.net.InetSocketAddress;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @ClassName ClientFactory
 * @Description TODO
 * @Author: XMK
 * @Date: 2020/9/30 9:47
 * @Version: 1.0
 */
public class ClientFactory {
    private int poolSize = 20;
    private ConcurrentHashMap<InetSocketAddress, ClientPool> poolHashMap = new ConcurrentHashMap<>();

    private ClientFactory() {
    }

    public static ClientFactory factory = new ClientFactory();

    public NioSocketChannel getClient(InetSocketAddress address) {

        ClientPool clientPool = poolHashMap.get(address);
        if (clientPool == null) {
            poolHashMap.putIfAbsent(address, new ClientPool(poolSize));
            clientPool = poolHashMap.get(address);
        }

        NioSocketChannel client = clientPool.getClient(address);
//        System.out.println("getClient : " + client);

        return client;

    }
}
